﻿var KanSea_pageindex = function(divid, articlecount, pagesize, page) {
	var allitemcount = $("#search_all_items_num span").text();
	var allitemcount = Number(allitemcount);
	if ($.browser.msie && $.browser.version != "8.0" && $.browser.version != "9.0") {
		articlecount = isnumber(articlecount) ? articlecount: 1;
	}
	if (articlecount <= 1) articlecount = 1;
	var KanSea_page_num = 5;
	var Charset_Previous = "Prev";
	var Charset_Next = "Next";
	var Charset_More = "More";
	var KanSea_num;
	var KanSea_pagelist_html = "";
	var pageindex = Math.ceil(articlecount / pagesize);
	if (IsLastPage) {
		IsLastPage = false;
		//page = pageindex;//last page
		page = oldpageindex + 1; //old pagecount+1
		//page += 1;//page +1 
		page = page > pageindex ? pageindex: page //old pagecount+1>last page =last page
	}
	oldpageindex = pageindex;
	ChildpageNum = page;
	if (NowPageNum < 1) {
		NowPageNum = 1;
	}
	var StartPageNum = 0; //pageindex*(NowPageNum-1);
	prevNum = NowPageNum;
	nextNum = NowPageNum;
	nextNum++;
	prevNum--;
	if (page == 1) {
		KanSea_pagelist_html += "<a href=\"javascript:void(0);\" class=\"prevdisable\">" + Charset_Previous + "</a>";
	} else KanSea_pagelist_html += "<a href=\"javascript:KanSea_pageindex('" + divid + "'," + articlecount + "," + pagesize + "," + (page - 1) + ");\" class=\"prev\">" + Charset_Previous + "</a>";
	prvepagenum = 0;
	addpagenum = 0;
	if ((page - KanSea_page_num) <= 0) {
		addpagenum = KanSea_page_num - page + 1;
	}
	if ((pageindex - page) < KanSea_page_num) {
		prvepagenum = KanSea_page_num - pageindex + page;
	}
	for (i = 0; i < (KanSea_page_num * 2 + 1) + addpagenum; i++) {
		KanSea_num = page - KanSea_page_num + i - prvepagenum;
		if (KanSea_num > 0 && KanSea_num <= pageindex) {
			KanSea_pagelist_html += "<a href=\"javascript:";
			if (KanSea_num == page) {
				KanSea_Show_Page(articlecount, pagesize, page);
				KanSea_pagelist_html += "void(0);\" class=\"curPage\">" + (KanSea_num + StartPageNum);
			} else {
				KanSea_pagelist_html += "KanSea_pageindex('" + divid + "'," + articlecount + "," + pagesize + "," + KanSea_num + ");\">" + (KanSea_num + StartPageNum);
			}
			KanSea_pagelist_html += "</a>";
		}
	}
	if ((articlecount + 1) < allitemcount) {
		KanSea_pagelist_html += "<a href=\"javascript:KanSea_Show_More(" + nextNum + ",true);\">...</a>";
	}
	/*
  if((pageindex-KanSea_page_num)>page){
    KanSea_pagelist_html += "<a href=\"javascript:KanSea_pageindex('"+divid+"',"+articlecount+","+pagesize+","+pageindex+");\">"+pageindex+"</a>";
  }
  */
	//KanSea_pagelist_html +="</div><div class=\"cnt\">";
	if (page == pageindex) {
		KanSea_pagelist_html += "<a href=\"javascript:void(0);\" class=\"nextdisable\">" + Charset_Next + "</a>";
	} else KanSea_pagelist_html += "<a href=\"javascript:KanSea_pageindex('" + divid + "'," + articlecount + "," + pagesize + "," + (page + 1) + ");\" class=\"next\">" + Charset_Next + "</a>";
	//if((articlecount+1)<Number(NowItemsNumTemp.replace(",",""))){
	if ((articlecount + 1) < allitemcount) {
		KanSea_pagelist_html += "<a href=\"javascript:KanSea_Show_More(" + nextNum + ");\" class=\"next\">" + Charset_More + "</a>";
	} else {
		KanSea_pagelist_html += "<a href=\"javascript:void(0);\" class=\"nextdisable\">" + Charset_More + "</a>";
	}
	//KanSea_pagelist_html+="</div><div class=\"line_clear\"></div></div>";
	//alert(KanSea_pagelist_html);
	var divwidth = 285;
	if (pageindex > 8) {
		divwidth += 370;
	} else {
		divwidth += (pageindex * 34);
	}
	//$("#"+divid).css({"width":divwidth+"px","margin-left":(980-divwidth)/2+"px",background:"red"});
	$("#" + divid).css({
		"width": divwidth + "px",
		"margin-left": (980 - divwidth) / 2 + "px"
	});
	$("#" + divid).html(KanSea_pagelist_html);
	$("#" + divid + "b").css({
		"width": divwidth + "px",
		"margin-left": (980 - divwidth) / 2 + "px"
	});
	$("#" + divid + "b").html(KanSea_pagelist_html);
	checkimgload("#search_result .item_main_images:visible", 100, 100);
	fiximagessize("#search_result .item_main_images:visible", 100, 100);
};
var KanSea_Show_Page = function(articlecount, pagesize, page) {
	for (j = 0; j <= articlecount; j++) {
		if (j >= (page - 1) * pagesize && j < page * pagesize) {
			$("#search_result .items").eq(j).show();
			//$("#items_"+j).show();
		} else {
			$("#search_result .items").eq(j).hide();
			//$("#items_"+j).hide();
		}
	}
	if ($.browser.mozilla) {
		$(".item_title").each(function() {
			$(this).wordLimit(17);
		});
	}
	if ($("#ShowType_B_S").val() == "b") {
		show_item_content();
	}
	//fiximagessize($("#search_result").find("img.item_main_images"),100,100);
};
var KanSea_Show_More = function(page, islast) {
	if (islast) {
		IsLastPage = true;
	}
	querystring = RequestQuerystring("p", page);
	querystring = RequestQuerystring("lang", search_lang, querystring);
	querystring = querystring.substring(querystring.indexOf("?") + 1);
	NowPageNum = page;
	$.ajax({
		url: "./WebService/search.php?dom=JSON&" + querystring,
		cache: false,
		dataType: "json",
		beforeSend: function() {
			openloading();
			/*
			if ($("#loading").size() > 0) {
				$("#loading").show();
			} else {
				$(".header").before("<div id=\"loading\"><img src=\"template//images/indicator.gif\"/>loading</div>");
			}
			*/
		},
		success: function(html) {
			SetJsonData(html, $("#ShowType_B_S").val());
			$("#floatbox_box").remove();
			$("#fjmybox").remove();
			/*
			$("#loading").hide();
			*/
		}
	});
};
function isnumber(val) {
	return /^-?((\d+\.?\d?)|(\.\d+))$/.test(val);
}